import React, { Component } from 'react'
import { fetchData } from '../../../../components/AjaxConsumer'
import { Input, Select } from 'antd';


const Option = Select.Option;

export default class SelectDynamic extends Component {
    constructor(props) {
        super(props);
        this.state = {
            value: "",
            selectOption: []
        };
        this.columnKey = this.props.name;
        this.mixSearch = this.props.mixSearch || false;
        this.mixinputName = this.props.mixinputName;
    }
    componentDidMount() {
        this.getListData(this.props.conditonData);
    }
    //获取数据
    getListData(conditonData) {
        const options = {
            url: this.props.url,
            type: 'get',
            condition: {
                ...conditonData
            }
        };
        fetchData(this, options, (resData) => {
            if (resData.code == "1") {
                this.setState({
                    selectOption: resData.data,
                });
                console.log("this.state.selectOption");
                console.log(this.state.selectOption);
            } else {
                console.log("请求数据错误：", resData.message);
            }
        });
    }

    onChange = (e) => {
        let inputValue = e.target.value;
        let inputName = e.target.name;
        let condition = [];
        condition[inputName] = inputValue;
        this.props.selectDynamicCallback(condition);
        this.setState({
            value: inputValue
        });
    }

     // 混合下拉输入
     mixChangeHandle = (e) => {
        let inputValue = e;
        let inputName = this.mixinputName;
        let condition = [];
        condition[inputName] = inputValue;
        this.props.selectDynamicCallback(condition);
        this.setState({
            value: inputValue
        });
    }

    componentWillReceiveProps(nextProps) {
        let valueDynamic = nextProps.valueDynamic;
        if (!valueDynamic) {
            this.setState({
                value: ''
            });
        }
    }

    render() {
        let selectOption = this.state.selectOption || [];
        // if (selectOption === null || selectOption.length < 0) {
        //     selectOption = [];
        // }

        let optionItem;
        if (this.mixSearch) {
            if (selectOption.length > 0) {
                optionItem = selectOption.map((e, index) => {
                    this.mixdicCode = e.dicCode;
                    return <Option key={index} value={e.dicName}>{e.dicName}</Option>
                });
            } else {
                optionItem = <Option value=""></Option>
            }
        } else {
            optionItem = selectOption.map((e, index) => {
                return <option key={index} value={e.dicCode}>{e.dicName}</option>
            });
        }

        return (
            this.mixSearch ?
                <Select
                    mode="combobox"
                    defaultValue=""
                    style={{ width: 80 }}
                    onChange={this.mixChangeHandle}
                >
                    {optionItem}
                </Select>
                :
                <select onChange={this.onChange} name={this.columnKey} value={this.state.value} style={{ width: this.props.entryWidth, height: 25 }}>
                    <option value=""></option>
                    {optionItem}
                </select>
        )
    }
}
